package xin.altitude.spring.sharding.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import xin.altitude.cms.common.entity.AjaxResult;
import xin.altitude.spring.sharding.domain.Order;
import xin.altitude.spring.sharding.mapper.OrderMapper;

/**
 * @author explore
 * @since 2022/02/28 15:00
 **/
@RestController
public class OrderController {
    @Autowired
    private OrderMapper orderMapper;
    
    /**
     * 以用户ID为条件分页查询
     *
     * @param page
     * @param order
     * @return
     */
    @GetMapping("/page")
    public AjaxResult page(Page<Order> page, Order order) {
        return AjaxResult.success(orderMapper.selectPage(page, Wrappers.lambdaQuery(order)));
    }
    
    /**
     * 以用户ID为条件列表查询
     *
     * @param order
     * @return
     */
    @GetMapping("/list")
    public AjaxResult list(Order order) {
        LambdaQueryWrapper<Order> wrapper = Wrappers.lambdaQuery(order);
        return AjaxResult.success(orderMapper.selectList(wrapper));
    }
    
    /**
     * 根据ID查询详情
     *
     * @param orderId
     * @return
     */
    @GetMapping("/detail/{orderId}")
    public AjaxResult detail(@PathVariable Long orderId) {
        return AjaxResult.success(orderMapper.selectById(orderId));
    }
    
    /**
     * 逻辑删除订单
     *
     * @param orderId
     * @return
     */
    @DeleteMapping("/delete/{orderId}")
    public AjaxResult delete(@PathVariable Long orderId) {
        return AjaxResult.success(orderMapper.deleteById(orderId));
    }
    
    /**
     * 更新订单状态
     *
     * @return
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Order order) {
        return AjaxResult.success(orderMapper.updateById(order));
    }
}
